package com.amoyt.project.enums;

import com.amoyt.project.domain.vo.ProjectLevelVo;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

import java.util.Arrays;
import java.util.List;

/**
 * @Date: 2025/8/25
 * @Time: 10:39
 * @Author: TQ_QT
 * @Description: 项目级别
 */
@NoArgsConstructor
@AllArgsConstructor
public enum ProjectLevelEnum {
    //村级项目
    VILLAGE_PROJECT(6,"village","village_admin", "村级项目"),
    //镇级项目
    TOWN_PROJECT(5,"town","town_admin", "镇级项目"),
    //县区级项目
    REGION_PROJECT(4,"region","region_admin", "县区级项目"),
    //市级项目
    CITY_PROJECT(3,"city","city_admin", "市级项目"),
    //省级项目
    PROV_PROJECT(2,"prov", "prov_admin","省级项目"),
    //国家级项目
    NATIONAL_PROJECT(1,"national","super_admin","国家级项目");

    private Integer order;
    private String level; //项目级别
    private String unit; //对应单位级别
    private String desc; //级别描述

    //项目状态常量 整体状态
    /**
     * 待审批
     */
    public static String STATUS_PENDING = "pending";
    /**
     * 审批中
     */
    public static String STATUS_UNDER_APPROVAL = "underApproval";
    /**
     * 通过
     */
    public static String STATUS_PASSED = "passed";
    /**
     * 驳回
     */
    public static String STATUS_REJECTED = "rejected";

    //项目审批常量
    /**
     * 通过
     */
    public static String PROJECT_PASS = "PASS";
    /**
     * 驳回
     */

    public static String PROJECT_REJECT = "REJECT";

    /**
     * 判断当前单位是否可以申请指定级别的项目
     * 肯定得目标级别比自己大才需要申请啊
     * @param currUnit 当前单位-即角色
     * @param targetLevel 目标级别
     * @return
     */
    public static  boolean isCanApply(String currUnit, String targetLevel){
        ProjectLevelEnum currLevel = getByUnit(currUnit);
        ProjectLevelEnum targetProjectLevel = getByLevel(targetLevel);
        return currLevel.getOrder() > targetProjectLevel.getOrder();
    }

    /**
     * 获取当前单位(角色)可获取下级列表
     * @param currRole 例如super_admin
     * @return
     */
    public static List<String> getLowerLevel(String currRole){
        ProjectLevelEnum level = getByUnit(currRole);

        //只返回大于等于 level.order的对象的Level字符串列表
        return Arrays.stream(ProjectLevelEnum.values())
                .filter(e -> e.getOrder() > level.getOrder())
                .map(ProjectLevelEnum::getLevel)
                .toList();
    }

    /**
     * 获取当前单位(角色)可获取的审批列表，就是查看已审批和未审批的
     * @param currRole 例如super_admin
     * @return
     */
    public static List<String> getHigherLevel(String currRole){
        ProjectLevelEnum level = getByUnit(currRole);

        //只返回小于等于 level.order的对象的Level字符串列表
        return Arrays.stream(ProjectLevelEnum.values())
                .filter(e -> e.getOrder() <= level.getOrder())
                .map(ProjectLevelEnum::getLevel)
                .toList();
    }

    /**
     * 判断当前单位是否可以审批指定级别的项目
     * @param currUnit
     * @param targetLevel
     * @return
     */
    public static boolean isCanApproval(String currUnit, String targetLevel){
        return currUnit.equals(targetLevel);
    }

    /**
     * 根据当前申请或者审批单位，获取下一级审批级别字符串（就是角色权限）
     * @param currUnit
     * @return
     */
    public static String nextLevel(String currUnit){
        ProjectLevelEnum enumVar = getByUnit(currUnit);
        return getByOrder(enumVar.getOrder()-1).getUnit();
    }

    /**
     * 查询可申请项目级别
     * @param role
     * @return
     */
    public static List<ProjectLevelVo> queryProjectLevel(String role){
        ProjectLevelEnum currLevel = getByUnit(role);

        //order较小才行，因为order越大，级别越低
        return Arrays.stream(ProjectLevelEnum.values())
                .filter(e -> e.getOrder() < currLevel.getOrder())
                .map(e -> new ProjectLevelVo(e.getLevel(),e.getDesc()))
                .toList();
    }

    private static ProjectLevelEnum getByLevel(String level){
        for (ProjectLevelEnum value : ProjectLevelEnum.values()) {
            if (value.getLevel().equals(level)){
                return value;
            }
        }
        return null;
    }

    private static ProjectLevelEnum getByUnit(String unit){
        for (ProjectLevelEnum value : ProjectLevelEnum.values()) {
            if (value.getUnit().equals(unit)){
                return value;
            }
        }
        return null;
    }

    private static ProjectLevelEnum getByOrder(Integer order){
        if(order < 1){
            return null;
        }
        for (ProjectLevelEnum value : ProjectLevelEnum.values()) {
            if (value.getOrder()== order){
                return value;
            }
        }
        return null;
    }

    public static boolean isLastLevel(String currentApprovalLevel, String projectLevel) {
        return getByUnit(currentApprovalLevel).getOrder() == getByLevel(projectLevel).getOrder();
    }


    public Integer getOrder() {
        return order;
    }

    public void setOrder(Integer order) {
        this.order = order;
    }
    public String getLevel() {
        return level;
    }

    public void setLevel(String level) {
        this.level = level;
    }

    public String getUnit() {
        return unit;
    }

    public void setUnit(String unit) {
        this.unit = unit;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }
}
